package com.health.db.service.sm.impl;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

import com.health.db.dao.sm.ILoginNumLogDao;
import com.health.db.dao.sm.ILoginUserLogDao;
import com.health.db.dao.sm.IOnlineLogDao;
import com.health.db.dao.sm.IRegistLogDao;
import com.health.db.dao.sm.IReqLogDao;
import com.health.db.dao.sm.IVisitLogDao;
import com.health.db.dao.user.IUserLISTDao;
import com.health.db.model.LoginNumLog;
import com.health.db.model.LoginUserLog;
import com.health.db.model.LoginUserLogExt;
import com.health.db.model.OnlineLog;
import com.health.db.model.RegistLog;
import com.health.db.model.ReqLog;
import com.health.db.model.UserLIST;
import com.health.db.model.VisitLog;
import com.health.db.service.sm.IReqLogService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

public class ReqLogServiceImpl implements IReqLogService {

	IReqLogDao reqLogDao;
	IVisitLogDao visitLogDao ;
	IRegistLogDao registLogDao ;
	IOnlineLogDao onlineLogDao ;
	ILoginUserLogDao loginUserLogDao ;
	ILoginNumLogDao loginNumLogDao ;
	private IUserLISTDao userLISTDao;

	
	
	public IVisitLogDao getVisitLogDao() {
		return visitLogDao;
	}

	public void setVisitLogDao(IVisitLogDao visitLogDao) {
		this.visitLogDao = visitLogDao;
	}

	public IRegistLogDao getRegistLogDao() {
		return registLogDao;
	}

	public void setRegistLogDao(IRegistLogDao registLogDao) {
		this.registLogDao = registLogDao;
	}

	public IOnlineLogDao getOnlineLogDao() {
		return onlineLogDao;
	}

	public void setOnlineLogDao(IOnlineLogDao onlineLogDao) {
		this.onlineLogDao = onlineLogDao;
	}

	public ILoginUserLogDao getLoginUserLogDao() {
		return loginUserLogDao;
	}

	public void setLoginUserLogDao(ILoginUserLogDao loginUserLogDao) {
		this.loginUserLogDao = loginUserLogDao;
	}

	public ILoginNumLogDao getLoginNumLogDao() {
		return loginNumLogDao;
	}

	public void setLoginNumLogDao(ILoginNumLogDao loginNumLogDao) {
		this.loginNumLogDao = loginNumLogDao;
	}

	public IReqLogDao getReqLogDao() {
		return reqLogDao;
	}

	public void setReqLogDao(IReqLogDao reqLogDao) {
		this.reqLogDao = reqLogDao;
	}

	@Override
	public void insertBatch(List<ReqLog> record) {
		
		
		reqLogDao.insertBatch(record);
	}

	@Override
	public int deleteByPrimaryKey(Long reqLogId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(ReqLog record) {
		// TODO Auto-generated method stub
		return reqLogDao.insert(record);
	}

	@Override
	public ReqLog selectByPrimaryKey(Long reqLogId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKey(ReqLog record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<ReqLog> selectByCondition(ReqLog reqLogExt) {
		// TODO Auto-generated method stub
		return reqLogDao.selectByCondition(reqLogExt);
	}

	@Override
	public void insertAllLog(String  method,String timestamp,Object jsonBean)  {
		VisitLog visitLogRecord=new VisitLog();
		RegistLog registLogRecord= new RegistLog();
		OnlineLog onlineLogRecord= new OnlineLog();
		LoginUserLog loginUserLogRecord=new LoginUserLog();
		LoginNumLog loginNumLogRecord= new LoginNumLog();
		
		if(null==timestamp ||"".equals(timestamp) ||null==method ||"".equals(method) || null==jsonBean)
		{
			
			System.out.println("args is null,please check timestamp,method and jsonBean! ");
			System.out.println("args is null,please check timestamp,method and jsonBean! ");
			System.out.println("args is null,please check timestamp,method and jsonBean! ");
			return;
		}

		String datetime=timestamp;
		String year=timestamp.substring(0,4);
		String date=timestamp.substring(0,8);
		String hour=timestamp.substring(0,10);
		String minute=timestamp.substring(0,12);
		String second=timestamp.substring(0,14);
		
		
		visitLogRecord.setVisitDatetime(datetime);
		visitLogRecord.setVisitYear(year);
		visitLogRecord.setVisitDate(date);
		visitLogRecord.setVisitHour(hour);
		visitLogRecord.setVisitMinute(minute);
		visitLogRecord.setVisitSecond(second);
		
		registLogRecord.setRegistDatetime(datetime);
		registLogRecord.setRegistYear(year);
		registLogRecord.setRegistDate(date);
		registLogRecord.setRegistHour(hour);
		registLogRecord.setRegistMinute(minute);
		registLogRecord.setRegistSecond(second);
		
		
		onlineLogRecord.setOnlineDatetime(datetime);
		onlineLogRecord.setOnlineYear(year);
		onlineLogRecord.setOnlineDate(date);
		onlineLogRecord.setOnlineHour(hour);
		onlineLogRecord.setOnlineMinute(minute);
		onlineLogRecord.setOnlineSecond(second);
		
		
		
		loginUserLogRecord.setLoginDatetime(datetime);
		loginUserLogRecord.setLoginYear(year);
		loginUserLogRecord.setLoginDate(date);
		loginUserLogRecord.setLoginHour(hour);
		loginUserLogRecord.setLoginMinute(minute);
		loginUserLogRecord.setLoginSecond(second);
		
		
		loginNumLogRecord.setLoginDatetime(datetime);
		loginNumLogRecord.setLoginYear(year);
		loginNumLogRecord.setLoginDate(date);
		loginNumLogRecord.setLoginHour(hour);
		loginNumLogRecord.setLoginMinute(minute);
		loginNumLogRecord.setLoginSecond(second);
		
		
		
		 
		 
		if(method.equals("login"))
		{
			//得到每个类，然后从中取到userId
			Field userNo = null;
			String userNofieldValue=null;
			try {
				userNo = jsonBean.getClass().getDeclaredField("userNo");
			
			
			userNo.setAccessible(true);//允许访问私有字段  
		    
			userNofieldValue = (String) userNo.get(jsonBean);//获得私有字段值  
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("userNofieldValue = " + userNofieldValue);  
			
			 //visitLogDao.insert(visitLogRecord);
			 //registLogDao.insert(registLogRecord);
			 //onlineLogDao.insert(onlineLogRecord);
			 loginUserLogRecord.setUserNo(userNofieldValue);
			 loginNumLogRecord.setUserNo(userNofieldValue);
			 
			 
			 LoginUserLogExt  query= new LoginUserLogExt();
			 query.setUserNo(userNofieldValue);
			 query.setLoginDate(date);
			 
			 
			  List<LoginUserLog> loginUserLogList= loginUserLogDao.selectByCondition(query);
			  if(null==loginUserLogList || loginUserLogList.size()==0)
			  {
				  
					 //检查是否今天已登录过，登录过则不写登录用户记录表
					 loginUserLogDao.insert(loginUserLogRecord);
			  }
			 
			
			 
			 loginNumLogDao.insert(loginNumLogRecord);
			
		}
		else if(method.equals("register"))
		{
			//得到每个类，然后从中取到userId
			Field userNo = null;
			String userNofieldValue=null;
			try {
				userNo = jsonBean.getClass().getDeclaredField("userNo");
				userNo.setAccessible(true);//允许访问私有字段  
				userNofieldValue = (String) userNo.get(jsonBean);//获得私有字段值  
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		    
			  
			System.out.println("userNofieldValue = " + userNofieldValue);  
			
			registLogRecord.setUserNo(userNofieldValue);
			
			
			UserLIST user=userLISTDao.selectByUserNo(userNofieldValue);
			if(null!=user)
			{
				 registLogDao.insert(registLogRecord);
			}
			 //visitLogDao.insert(visitLogRecord);
			
			// onlineLogDao.insert(onlineLogRecord);
			// loginUserLogDao.insert(loginUserLogRecord);
			// loginNumLogDao.insert(loginNumLogRecord);
		}
		else
		{
			
			
			Field userId = null;
			Field userID= null;;
			Field userid= null;;
			String userIdfieldValue = null;
			String userIDfieldValue = null;
			String useridfieldValue = null;
			try {
				userId = jsonBean.getClass().getDeclaredField("userId");
	 
				userId.setAccessible(true);//允许访问私有字段  
			 
				 userIdfieldValue =  String.valueOf( userId.get(jsonBean));//获得私有字段值  
	 
				
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
			try {
				 
				userID=jsonBean.getClass().getDeclaredField("userID");
		 
				 
				userID.setAccessible(true);//允许访问私有字段 
	 
				 
				 userIDfieldValue =  String.valueOf(userID.get(jsonBean));//获得私有字段值  
	 
				
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
			 
				userid=jsonBean.getClass().getDeclaredField("userid");
				 
				userid.setAccessible(true);//允许访问私有字段 
			 
				 useridfieldValue =  String.valueOf(userid.get(jsonBean));//获得私有字段值  
				
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			System.out.println("userIdfieldValue = " + userIdfieldValue); 
			System.out.println("userIDfieldValue = " + userIDfieldValue); 
			System.out.println("useridfieldValue = " + useridfieldValue); 
			
			if(null==userIdfieldValue && null==userIDfieldValue && null==useridfieldValue)
			{
				//得到每个类，然后从中取到userId
				Field memberId;
				String memberIdfieldValue = null;
				try {
					memberId = jsonBean.getClass().getDeclaredField("memberId");
					userId.setAccessible(true);//允许访问私有字段  
				    
					memberIdfieldValue = String.valueOf( memberId.get(jsonBean));//获得私有字段值  
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				System.out.println("memberIdfieldValue = " + memberIdfieldValue);  
				
				if(null==memberIdfieldValue)
				{
					visitLogRecord.setUserId(memberIdfieldValue);
					onlineLogRecord.setUserId(memberIdfieldValue);
					//检查用户的上一次登录时间，看否要对登录进行记录
					
					 visitLogDao.insert(visitLogRecord);
					 UserLIST user=userLISTDao.selectByUserId(Long.valueOf(memberIdfieldValue));
					 if(null!=user)
					 {
						 
						 Date ud= user.getUpdateDate();
						 
						 boolean result=DateUtils.judgmentDate(ud, new Date(), 0.5);
						 if(result)
						 {
							 onlineLogDao.insert(onlineLogRecord);
						 }
						
					 }
					// loginUserLogDao.insert(loginUserLogRecord);
					// loginNumLogDao.insert(loginNumLogRecord);
				}
				
			}
			
			else
			{
				
				if(null!=userIdfieldValue)
				{
					
					
					visitLogRecord.setUserId(userIdfieldValue);
					onlineLogRecord.setUserId(userIdfieldValue);
					//检查用户的上一次登录时间，看否要对登录进行记录
					 visitLogDao.insert(visitLogRecord);
					 
					 UserLIST user=userLISTDao.selectByUserId(Long.valueOf(userIdfieldValue));
					 if(null!=user)
					 {
						 
						 Date ud= user.getUpdateDate();
						 
						 boolean result=DateUtils.judgmentDate(ud, new Date(), 0.5);
						 if(result)
						 {
							 onlineLogDao.insert(onlineLogRecord);
						 }
						
					 }
					// loginUserLogDao.insert(loginUserLogRecord);
					// loginNumLogDao.insert(loginNumLogRecord);
				}
				else if(null!=userIDfieldValue)
				{
					
					visitLogRecord.setUserId(userIDfieldValue);
					onlineLogRecord.setUserId(userIDfieldValue);
					//检查用户的上一次登录时间，看否要对登录进行记录
					 visitLogDao.insert(visitLogRecord);
					  
					 UserLIST user=userLISTDao.selectByUserId(Long.valueOf(userIDfieldValue));
					 if(null!=user)
					 {
						 
						 Date ud= user.getUpdateDate();
						 
						 boolean result=DateUtils.judgmentDate(ud, new Date(), 0.5);
						 if(result)
						 {
							 onlineLogDao.insert(onlineLogRecord);
						 }
						
					 }
					// loginUserLogDao.insert(loginUserLogRecord);
					 //loginNumLogDao.insert(loginNumLogRecord);
				}//
				else if(null!=useridfieldValue)
				{
					visitLogRecord.setUserId(useridfieldValue);
					onlineLogRecord.setUserId(useridfieldValue);
					//检查用户的上一次登录时间，看否要对登录进行记录
					 visitLogDao.insert(visitLogRecord);
					 
					 UserLIST user=userLISTDao.selectByUserId(Long.valueOf(useridfieldValue));
					 if(null!=user)
					 {
						 
						 Date ud= user.getUpdateDate();
						 
						 boolean result=DateUtils.judgmentDate(ud, new Date(), 0.5);
						 if(result)
						 {
							 onlineLogDao.insert(onlineLogRecord);
						 }
						
					 }
					 //loginUserLogDao.insert(loginUserLogRecord);
					// loginNumLogDao.insert(loginNumLogRecord);
				}
			}
		}
		
		
		
		
		
		
	}

	@Override
	public int updateState(ReqLog record) {
		
		record.setState(Constant.CONSTANTS_REQ_LOG_STATE_1);
		// TODO Auto-generated method stub
		return reqLogDao.updateByPrimaryKey(record);
	}

}
